package com.libservice

import android.app.Application
import com.libbase.Wrapper
import com.libbase.datarepository.DataRepositoryManager
import com.libservice.activityservice.ActivityService
import com.libservice.activityservice.ActivityServiceInterface
import com.libservice.cacheservice.CacheService
import com.libservice.cacheservice.CacheServiceInterface
import com.libservice.datarepositories.AuthRepository
import com.libservice.datarepositories.DeviceModelRepository
import com.libservice.eventtracking.EventTracking
import com.libservice.eventtracking.EventTrackingInterface
import com.libservice.loggerservice.LoggerService
import com.libservice.loggerservice.LoggerServiceInterface
import com.libservice.screenshotservice.ScreenshotService
import com.libservice.webviewmessageservice.WebViewMessageService
import com.libservice.webviewmessageservice.WebViewMessageServiceInterface
import io.reactivex.rxjava3.core.Observable

class ServiceManager private constructor() {
    companion object {
        val instance: ServiceManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ServiceManager()
        }
    }

    val deviceModelRepository: DeviceModelRepository
        get() = DataRepositoryManager.instance.repository(DeviceModelRepository.builder.id) as DeviceModelRepository

    val userAgreementVersion: Observable<Wrapper<String>>
        get() = deviceModelRepository.dataObserver.map { Wrapper(it.value?.userAgreementVersion) }

    val guideVersion: Observable<Wrapper<String>>
        get() = deviceModelRepository.dataObserver.map { Wrapper(it.value?.guideVersion) }

    var currentGuideVersion: String? = null

    val loggerService: LoggerServiceInterface = LoggerService()

    val webViewMessageService: WebViewMessageServiceInterface = WebViewMessageService()

    lateinit var eventTracking: EventTrackingInterface

    lateinit var flutterService: FlutterServiceInterface
        private set

    lateinit var routerService: RouterServiceInterface
        private set

    lateinit var authService: AuthServiceInterface
        private set

    lateinit var activityService: ActivityServiceInterface
        private set

    lateinit var cacheService: CacheServiceInterface
        private set

    val screenShotService: ScreenshotServiceInterface = ScreenshotService()

    init {
        DataRepositoryManager.instance.register(DeviceModelRepository.builder)
        DataRepositoryManager.instance.register(AuthRepository.builder)
    }

    fun initService(application: Application) {
        activityService = ActivityService(application)

        cacheService = CacheService()

        eventTracking = EventTracking()
    }

    fun setFlutterService(flutterService: FlutterServiceInterface) {
        this.flutterService = flutterService
    }

    fun setRouterService(routerService: RouterServiceInterface) {
        this.routerService = routerService
    }

    fun setAuthService(authService: AuthServiceInterface) {
        this.authService = authService
        cacheService.setUserCache()
    }
}